﻿/*
 * Copyright 2009 - 2010 eScience Institute
 *
 * This file is part of SqlShare.
 *
 * SqlShare is free software: you can redistribute it and/or modify it under the terms 
 * of the GNU General Public License as published by the Free Software Foundation, 
 * either version 3 of the License, or (at your option) any later version.
 *
 * SqlShare is distributed in the hope that it will be useful, but WITHOUT ANY 
 * WARRANTY; without even the implied warranty of MERCHANTABILITY or FITNESS FOR 
 * A PARTICULAR PURPOSE. See the GNU General Public License for more details.
 *
 * You should have received a copy of the GNU General Public License along with 
 * SqlShare. If not, see http://www.gnu.org/licenses/.
*/

/*
 * ******** IMPORTANT ****** 
 * This file and the ResponseMessage in WebQueryRole Must be identical, any changes 
 * must be reflected in the other file (cannot reference project or receive a compile error)
 */
using System;
using System.Net;
using System.Xml.Serialization;
using System.Text;
using System.Collections;
using System.Collections.Generic;
using System.IO;

namespace TestSL3
{
    public class ResponseMessage
    {
        [XmlElement]
        public string Error { get; set; }
        [XmlIgnore]
        public Dictionary<string, object> Params { get; set; }
        [XmlArray("Params")]
        [XmlArrayItem("ParamItem", Type = typeof(DictionaryEntry))]
        public DictionaryEntry[] _x_Stuff
        {
            get
            {
                // make an array of ditionary entries to pass through serialization
                DictionaryEntry[] ret = new DictionaryEntry[Params.Count];
                int i = 0;
                DictionaryEntry de;
                foreach (KeyValuePair<string, object> p in Params)
                {
                    de = new DictionaryEntry();
                    de.Key = p.Key;
                    de.Value = p.Value;
                    ret[i++] = de;
                }

                return ret;
            }

            set
            {
                Params.Clear();
                for (int i = 0; i < value.Length; i++)
                {
                    Params.Add((string)value[i].Key, value[i].Value);
                }
            }
        }

        public ResponseMessage()
        {
            Error = String.Empty;
            Params = new Dictionary<string, object>();
        }

        public void WriteToStream(Stream stream)
        {
            XmlSerializer xs = new XmlSerializer(typeof(ResponseMessage));
            xs.Serialize(stream, this);
        }

        public static ResponseMessage ReadFromStream(Stream stream)
        {
            XmlSerializer xs = new XmlSerializer(typeof(ResponseMessage));
            return (ResponseMessage)xs.Deserialize(stream);
        }
    }
}
